<?php
/**
 * Class IndexController
 */
class AdminController extends AppControllerAction
{
    function init()
    {
        parent::init();  
        
        $userModel = new Users();
        
        if(!$userModel->isAuthenticated()) {
            $this->_redirect();              
        }  else {
            $id = $userModel->getCurrentUserInfor()->id;
            $userModel->getById($id);
            
            if($userModel->getType() != ADMIN)  
                $this->_redirect(); 
        }
        $this->_helper->layout->setLayout('admin');   
    } 
    
    function indexAction()
    {
        $propertyModel = new Property();
        $this->view->property = $propertyModel->getListProperty()->toArray();
        
  	    $this->render("office");    
    }    
    
   
    function officecreateAction()
    {
        $id = $this->_request->getParam('id',0);
        
        $property = new Property();
        $this->view->property = $property->getById($id)->toArray();        
        
        $city = new City();
        $this->view->city = $city->getListCity()->toArray(); 
    }
    
    function officecreateexcuteAction()
    {
        $id = $this->_request->getParam('id',0);
        if($this->_request->isPost())  
        {
            $property = new Property();
            
            $property->getById($id);
            $property->setNameEn($this->_request->getParam('name_en','default'));
            $property->setNameVi($this->_request->getParam('name_vi','default'));
            $property->setShortDescriptionEn($this->_request->getParam('short_description_en','default'));
            $property->setShortDescriptionVi($this->_request->getParam('short_description_vi','default'));
            $property->setDescriptionEn($this->_request->getParam('description_en','default'));
            $property->setDescriptionVi($this->_request->getParam('description_vi','default'));
            $property->setStateEn($this->_request->getParam('state_en','default'));
            $property->setStateVi($this->_request->getParam('state_vi','default'));
            $property->setStreetEn($this->_request->getParam('street_en','default'));
            $property->setStreetVi($this->_request->getParam('street_vi','default'));
            $property->setArea($this->_request->getParam('area',0));
            $property->setPrice($this->_request->getParam('price',0));
            $property->setWebsite($this->_request->getParam('description_en',''));
            $property->setContactEmail($this->_request->getParam('description_vi',''));
            $property->setContactPhone($this->_request->getParam('description_vi',''));
           // $property->setImage($this->_request->getParam('description_vi','default'));
            
            $property->save();              
        }
        
        $this->_redirect('admin');
    }
    
    function userAction()
    {
        $user = new Users();
        
        $this->view->user = $user->getListUsers()->toArray();
    }
    
    function usercreateAction()
    {
        $id = $this->_request->getParam('id',0);
        
        $user = new Users();
        $this->view->user = $user->getById($id)->toArray();         
    }
    
    function usercreateexcuteAction()
    {
        $id = $this->_request->getParam('id',0);
        
        $user = new Users();
        $user->getById($id);         
        $user->setUsername($this->_request->getParam('username',''));
        $user->setPassword(md5($this->_request->getParam('password','')));
        $user->setFullname($this->_request->getParam('fullname',''));     
        $user->setEmail($this->_request->getParam('email',''));     
        $user->setGender($this->_request->getParam('gender',1));
        $user->setAddress($this->_request->getParam('address',''));
        $user->setTelephone($this->_request->getParam('telephone',''));

        $user->save();
        
        $this->_redirect('admin/user');    
    }
    
    function userdeleteAction()
    {
        $id = $this->_request->getParam('id',0);
        
        $user = new Users();         
        $user->delete('id = '.$id);
        
        $this->_redirect('admin/user');        
    }
    
    function businesscategoryAction()
    {
        $category = new ServiceCategory();
        $categories = $category->getListServiceCategory()->toArray();
        
        $business = new Service();
        foreach($categories as &$cate)
        {            
            $cate['total'] = $business->numRows(' category = '.$cate['id']);
        }
        
        $this->view->categories = $categories;
    }
    
    function businesscategorycreateAction()
    {
        $id = $this->_request->getParam('id',0);
        
        $category = new ServiceCategory();      
        $this->view->category = $category->getById($id)->toArray();       
    }
    
    function businesscategorycreateexcuteAction()
    {
        $id = $this->_request->getParam('id',0);
        
        $category = new ServiceCategory();      
        $category->getById($id);
        
        $category->setNameEn($this->_request->getParam('name_en','empty'));
        $category->setNameVi($this->_request->getParam('name_vi','empty'));
        $category->save();
        
        $this->_redirect('admin/businesscategory');
    }   
    
    function businesscategorydeleteAction()
    {
        $id = $this->_request->getParam('id',0);
        
        $category = new ServiceCategory(); 
        $category->delete('id = '.$id);
        
        $this->_redirect('admin/businesscategory');     
    }
    
    function businesscategorydetailsAction()
    {
        $id = $this->_request->getParam('id',0);
        
        $this->view->cate = $id;
        $business = new Service();      
        $this->view->business = $business->getListService('category='.$id)->toArray();  
    }
    
    function businesscreateAction()
    {
        $id = $this->_request->getParam('id',0);        
        $this->view->cate = $this->_request->getParam('cate');      
                 
        $business = new Service();      
        $this->view->business = $business->getById($id)->toArray();  
    }
    
    function businesscreateexcuteAction()
    {  
        $id = $this->_request->getParam('id',0);
        
        $business = new Service();      
        $business->getById($id);
        
        $business->setCategory($this->_request->getParam('cate'));          
        $business->setTitleEn($this->_request->getParam('title_en','empty'));
        $business->setTitleVi($this->_request->getParam('title_vi','empty'));
        $business->setShortContentEn($this->_request->getParam('short_content_en','empty'));
        $business->setShortContentVi($this->_request->getParam('short_content_vi','empty'));
        $business->setDescriptionEn($this->_request->getParam('description_en','empty'));
        $business->setDescriptionVi($this->_request->getParam('description_vi','empty'));
        $business->setAddressEn($this->_request->getParam('address_en','empty'));
        $business->setAddressVi($this->_request->getParam('address_vi','empty'));
        $business->setPhone($this->_request->getParam('phone','empty'));
        $business->setWebsite($this->_request->getParam('website','empty'));
       // $business->setImage($this->_request->getParam('title_vi','empty'));
        
        
        $business->save();
        
        $this->_redirect('admin/businesscategorydetails/id/'.$this->_request->getParam('cate'));      
    }
    
    function businessdeleteAction()
    {
        $id = $this->_request->getParam('id',0);
                
        $business = new Service();      
        $business->getById($id);
        $cate = $business->getCategory();
        $this->view->business = $business->delete('id = '.$id);  
       
       $this->_redirect('admin/businesscategorydetails/id/'.$cate); 
    }
    
    function relaxcategoryAction()
    {
        $category = new RelaxCategory();
        $categories = $category->getListRelaxCategory()->toArray();
        
        $relax = new Relax();
        foreach($categories as &$cate)
        {            
            $cate['total'] = $relax->numRows(' category = '.$cate['id']);
        }
        
        $this->view->categories = $categories;
    }
    
    function relaxcategorycreateAction()
    {
        $id = $this->_request->getParam('id',0);
        
        $category = new RelaxCategory();      
        $this->view->category = $category->getById($id)->toArray();       
    }
    
    function relaxcategorycreateexcuteAction()
    {
        $id = $this->_request->getParam('id',0);
        
        $category = new RelaxCategory();      
        $category->getById($id);
        
        $category->setNameEn($this->_request->getParam('name_en','empty'));
        $category->setNameVi($this->_request->getParam('name_vi','empty'));
        $category->save();
        
        $this->_redirect('admin/relaxcategory');
    }
    
    function relaxcategorydeleteAction()
    {
        $id = $this->_request->getParam('id',0);
        
        $category = new RelaxCategory(); 
        $category->delete('id = '.$id);
        
        $this->_redirect('admin/relaxcategory');    
    }
    
    function relaxcategorydetailsAction()
    {
        $id = $this->_request->getParam('id',0);
        
        $relax = new Relax();      
        $this->view->relax = $relax->getListRelax('category='.$id)->toArray();    
    }
    
    function relaxcreateAction()
    {
    }
    
    function relaxcreateexcuteAction()
    {
    }
    
    function relaxdeleteAction()
    {
    }
    
}
?>